package com.kaibes.admin.token;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.kaibes.core.base.annotation.Description;
import com.kaibes.module.token.service.TokenLogService;
import com.kaibes.module.token.service.TokenService;
import com.kaibes.web.admin.AdminController;
import com.kaibes.web.api.ApiLink;

@AdminController
@RequestMapping("/admin/token")
public class AdminTokenController {

    @Autowired
    private TokenService tokenService;
    @Autowired
    private TokenLogService tokenLogService;
    
    @GetMapping("manage")
    @ApiLink("令牌管理")
    public String manage(Model model, 
            @RequestParam(defaultValue = "25") @Description("每页长度") int size,
            @RequestParam(defaultValue = "1") @Description("当前页数") int page,
            @RequestParam(defaultValue = "") @Description("搜索内容") String search) {
        if (search.isEmpty()) {
            model.addAttribute("tokenPage", tokenService.page(page, size));
        } else {
            model.addAttribute("tokenPage", tokenService.pageByNicknameLike(page, size, search));
        }
        return "token-manage";
    }
    
    @GetMapping("log")
    @ApiLink("令牌日志")
    public String log(Model model, 
            @RequestParam(defaultValue = "25") @Description("每页长度") int size,
            @RequestParam(defaultValue = "1") @Description("当前页数") int page,
            @RequestParam(defaultValue = "") @Description("搜索内容") String search) {
        if (search.isEmpty()) {
            model.addAttribute("logPage", tokenLogService.page(page, size));
        } else {
            model.addAttribute("logPage", tokenLogService.pageByTypeOrNicknameLike(page, size, search));
        }
        return "token-log";
    }
    
}
